<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <button @click="isShow = !isShow">{{isShow ? '隐藏' : '显示' }}</button>
    <!-- 
      keep-alive将需要缓存的组件包裹起来，看起来与v-show一样，但是使用v-if
      被keep-alive的组件通过v-if隐藏并不会触发mounted函数和destroyed函数

      有两个钩子函数，用于监听keep-alive的执行过程 activated和deactivated
      activated：监听被keep-alive的组件被启用
      deactivated：监听被keep-alive的组件被隐藏
    -->
    <keep-alive>
      <child v-if="isShow"></child>
    </keep-alive>
  </div>
  <template id="child">
    <div>
      <input type="text" v-model="msg">
      {{msg}}
    </div>
  </template>
  <script src="../vue.js"></script>
  <script>
    const child = {
      template: '#child',
      data() {
        return {
          msg: ''
        }
      },
      mounted(){
        console.log('mounted');
      },
      destroyed(){
        console.log('destroyed');
      },
      activated(){
        console.log('activated');
      },
      deactivated(){
        console.log('deactivated');
      }
    }
    Vue.component('Child', child);
    const vm = new Vue({
      el: '#app',
      data: {
        isShow: true
      }
    })

  </script>
</body>

</html>